﻿// -----------------------------------------------------------------------
// <copyright file="LichHenServices.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ASS.BUS.Services
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using ASS.BUS.DTO;
    using ASS.DAO;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class LichHenServices
    {
        public bool Add(LichHen lichHen)
        {
            var addSuccsess = false;
            using (var db = new DataContext())
            {
                try
                {
                    if (db.LichHens != null) db.LichHens.Add(lichHen);
                    db.SaveChanges();
                    addSuccsess = true;
                }
                catch (Exception ex)
                {
                    addSuccsess = false;
                    MessageBox.Show(ex.Message);
                }
            }
            return addSuccsess;
        }

        public LichHen GetLichHen(int id)
        {
            using (var db = new DataContext())
            {
                var item = db.LichHens.Single(x => x.MaLh == id);
                return item;
            }
        }

        public List<LichHen> GetLichHens()
        {
            using (var db = new DataContext())
            {
                var items = db.LichHens.ToList();

                return items;
            }
        }

        public bool EditLichHen(LichHen lichHen)
        {
            using (var db = new DataContext())
            {
                var item = db.LichHens.FirstOrDefault(x => x.MaLh == lichHen.MaLh);

                if (item == null) return false;
                item.NgayHen = lichHen.NgayHen;
                item.NoiDung = lichHen.NoiDung;
                item.SoDienThoai = lichHen.SoDienThoai;
                item.BacSi = lichHen.BacSi;
                item.BenhNhan = lichHen.BenhNhan;
                db.SaveChanges();
                return true;
            }
        }

        public bool Delete(int Id)
        {
            using (var db = new DataContext())
            {
                var item = db.LichHens.FirstOrDefault(x => x.MaLh == Id);

                if (item == null) return false;
                db.LichHens.Remove(item);
                db.SaveChanges();
                return true;
            }
        }
    }
}
